package com.module.prms.manager;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.prms.ReqBaseBean;
import com.module.prms.dao.RoleDao;
import com.module.prms.vo.PrmsRole;

/**
*
* <权限-角色服务类>
* 
* @author  huhui
* @version  [1.0, July 15, 2011]
* @see  [相关类/方法]
* @since  [二代产品/权限组件]
*/	
public class RoleManager extends GenericHibernateManager<PrmsRole, String> {
	static Logger logger = Logger.getLogger(RoleManager.class.getName ()) ;
	RoleDao dao = RoleDao.getInstance();
	
	private RoleManager(){}
	
	private static RoleManager manager;
	
	public synchronized static RoleManager getInstance(){
		if(manager == null )
			manager = new RoleManager();
		return manager;
	} 
/**
 * <保存>
 * <将角色信息保存到数据库>
 * @param bean
 * @see [类、类#方法、类#成员]
 */
public boolean add(PrmsRole pr)throws Exception
{
	boolean isTrue = this.saveOrUpdate(pr);
	return isTrue;
}

/**
 * <删除>
 * <将角色信息从数据库删除>
 * @param bean
 * @see [类、类#方法、类#成员]
 */
public void del(PrmsRole obj)throws Exception
{
	this.delete(obj);
}



/**
 * <修改>
 * <修改数据库中的角色信息>
 * @param bean
 * @see [类、类#方法、类#成员]
 */
public void upd(PrmsRole obj)throws Exception
{
	this.update(obj);
}

/**
 * 列表
 * 列表显示数据库角色信息
 * @param bean
 * @see [类、类#方法、类#成员]
 */
public Page list(Page pageObj,PrmsRole pr)throws Exception
{
	Session session = this.getSession();
	Page page = null;
	try {
		page =  dao.getList(session,pr,pageObj.getStart(),pageObj.getLimit());
	} catch (Exception e) {
		throw e;
	} finally {
		this.closeSession();
	}
	return page;
}
/**
 * 根据组织id查询角色
 * @param bean
 * @throws Exception
 */
public Page getorgrole(PrmsRole pr)throws Exception
{
	Session session = this.getSession();
	List list = null;
	Page page = new Page();
	page.setStart(0);
	page.setTotal((long) 100);
	try{
		list = dao.getorgrole(session, pr.getOrgid());
	}catch(Exception e){
		throw e;
	}finally {
		this.closeSession();
	}
	page.setRows(list);
	return page;
}

/**
 * <根据id查找数据>
 * <列表显示数据库角色信息>
 * @param bean
 * @see [类、类#方法、类#成员]
 */
public PrmsRole findebyid(PrmsRole obj)throws Exception
{
	PrmsRole pr = this.get(obj.getCroleId());
	return pr;
}


/**
 * <根据id查找数据>
 * <列表显示数据库角色信息>
 * @param bean
 * @see [类、类#方法、类#成员]
 */
public PrmsRole finderolebyid(String id)throws Exception
{
	PrmsRole pr = this.get(id); 
	return pr;
}

/**
 * 根据角色名和组织查找
 * @param bean
 * @return
 */
public PrmsRole RoleFindByName(PrmsRole role)throws Exception{
	String name=role.getCroleName();
	String orgid=role.getOrgid();
	String hql ="from PrmsRole as obj where orgid='"+orgid+"' and croleName='"+ name+"'";
	PrmsRole pr = this.findUniqueObjectBySQL(hql);
	return pr;
}

/**
 * <根据id删除>
 * <将角色信息从数据库删除>
 * @param bean
 * @see [类、类#方法、类#成员]
 */
public boolean remove(List<String> idArray)throws Exception
{
	boolean isTrue = false;
	Session session = this.getSession();
	Transaction tx = null;
	tx = session.beginTransaction();
	try {
		for (String s : idArray) {
			isTrue = dao.deleteByKey(session,s);
		}
		tx.commit();
	} catch (Exception e) {
		tx.rollback();
		throw e;
	} finally {
		this.closeSession();
	}
	return isTrue;
}
@Override
public GenericHibernateDao<PrmsRole, String> getDao() {
	return dao;
}

}
